<?php
require_once 'base.php';

$tpl = new Template('t');
$tpl->setFile ( "main", "comment_edit.html" );
$tpl->setBlock ( "main", "list", "lists" );

$db = conn();

// ------------- 编辑 -------------
$edit = fget('edit', 0);
if ($edit){
	$cid_edit = del_space(fget('cid', 0));
	switch ($edit){
		case "shield": // 屏蔽
			$sql_edit = "UPDATE comment SET status=-1 WHERE id='{$cid_edit}'";
			if ($db->query($sql_edit)){
				$aid_edit = del_space(fget('aid', 0));
				if ($aid_edit){
					$sql_edit = "UPDATE article SET comment=comment-1 WHERE aid='{$aid_edit}'";
					if ($db->query($sql_edit)){
						$tpl->setVar("alert_edit", "alert(\"操作成功！\")");
					}else{
						$tpl->setVar("alert_edit", "alert(\"文章评论数更新失败！\")");
					}
				}else{
					$tpl->setVar("alert_edit", "alert(\"文章评论数更新失败！\")");
				}
			}else{
				$tpl->setVar("alert_edit", "alert(\"操作失败！\")");
			}
			break;
		case "recover": // 恢复
			$sql_edit = "UPDATE comment SET status=0 WHERE id='{$cid_edit}'";
			if ($db->query($sql_edit)){
				$aid_edit = del_space(fget('aid', 0));
				if ($aid_edit){
					$sql_edit = "UPDATE article SET comment=comment+1 WHERE aid='{$aid_edit}'";
					if ($db->query($sql_edit)){
						$tpl->setVar("alert_edit", "alert(\"操作成功！\")");
					}else{
						$tpl->setVar("alert_edit", "alert(\"文章评论数更新失败！\")");
					}
				}else{
					$tpl->setVar("alert_edit", "alert(\"文章评论数更新失败！\")");
				}
			}else{
				$tpl->setVar("alert_edit", "alert(\"操作失败！\")");
			}
			break;
	}
}
// ------------- 显示list -------------
$aid = del_space(fget('aid', 0));
$where = "";
if ($aid){
	$where = " WHERE aid='{$aid}'";
}

$sql = "SELECT COUNT(*) FROM comment" . $where;
$rs=$db->query($sql, MYSQL_NUM);

if($rs[0][0] > 0){
	$page_str = pageft($rs[0][0], 20);

	$sql = "SELECT * FROM comment" . $where . " ORDER BY id DESC {$page_str['limit']}";
	$rs=$db->query($sql, MYSQL_ASSOC);
	$i = 0;
	foreach($rs as $v){
		// 屏蔽、恢复文章
		if ($v['status'] > -1){
			$edit_str = "<a href=\"?edit=shield&cid={$v['id']}&aid={$v['aid']}\">屏蔽</a>";
		}else{
			$edit_str = "<a href=\"?edit=recover&cid={$v['id']}&aid={$v['aid']}\">恢复</a>";
		}
		// 行颜色
		$color_h = ($i++%2)>0 ? " class=\"color_h\"" : "";
		$tpl->setVar(array(
			"id" => $v['id'],
			"aid" => $v['aid'],
			"nickname" => $v['nickname'],
			"mail" => $v['mail'],
			"website" => $v['website'],
			"contents" => strip_tags($v['contents']),
			"contents_sub" => utf_substr(strip_tags($v['contents']), 20),
			"submit_time" => date("Y-m-d h:i:s", $v['submit_time']),
			"submit_ip" => long2ip($v['submit_ip']),
			"edit_str" => $edit_str,
			"color_h" => $color_h
		));

		$tpl->parse("lists", "list", true);
	}
	$tpl->setVar("page_str", $page_str["str"]);
}

$db->close();

$tpl->p("main");
?>